package com.shujia.sql

import org.apache.flink.table.api.{EnvironmentSettings, TableEnvironment}

object Demo7FLinkSqlToHbase {
  def main(args: Array[String]): Unit = {


    val setting: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .inStreamingMode()
      .build()

    val table: TableEnvironment = TableEnvironment.create(setting)

    table.executeSql(
      """
        |CREATE TABLE student (
        |  id STRING,
        |  name STRING,
        |  age INT,
        |  gender STRING,
        |  clazz STRING
        |)  WITH (
        |  'connector' = 'filesystem',           -- 必选：指定连接器类型
        |  'path' = 'data/students.json',        -- 必选：指定路径
        |  'format' = 'json'                     -- 必选：文件系统连接器指定 format
        |)
        |
      """.stripMargin)


    /**
      * 基于hbase 创建表
      *
      */

    table.executeSql(
      """
        |CREATE TABLE hbase_student (
        | id STRING,
        | info ROW<name STRING,age BIGINT,gender STRING,clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student', -- hbase中的表名
        | 'zookeeper.quorum' = 'master:2181' -- zookeeper地址
        |)
        |
      """.stripMargin)

    /**
      * 将查询的结果写入hbase表中
      *
      * 需要先启动hbase
      * 在hbase中创建表
      * create 'student','info'
      */

    table.executeSql(
      """
        |insert into hbase_student
        |select id,ROW(name,age,gender,clazz) as info from student
        |
      """.stripMargin)


  }

}
